#!/usr/bin/env python

import sys
import os
import ConfigParser

if sys.platform == 'win32':
    slash='\\'
    dotputty='.putty'
else:
    slash='/'
    dotputty='%s/.putty' % os.environ['HOME']
session_types={None:0, 'None':0, 'SOCKS 4':1,'SOCKS 5':2,'HTTP':3,'Telnet':4,'Local':5}
sessions_path=dotputty+slash+'sessions'

class putty_session(ConfigParser.ConfigParser):
    def __init__(self, defaults=None, filename=None,proxy_method=None, proxy_host=None, proxy_port=None,
                 proxy_username=None, proxy_password=None, proxy_command=None):
        if proxy_method:
            self.proxy_method=proxy_method
        else:
            self.proxy_method=None
        if proxy_host:
            self.proxy_host=proxy_host
        else:
            self.proxy_host=""
        if proxy_port:
            self.proxy_port = proxy_port
        else:
            self.proxy_port = -1
        if proxy_username:
            self.proxy_username=proxy_username
        else:
            self.proxy_username=""
        if proxy_password:
            self.proxy_password=proxy_password
        else:
            self.proxy_password=""
        if proxy_command:
            self.proxy_command=proxy_command
        else:
            self.proxy_command=""
        self.filename=sessions_path+slash+filename
        ConfigParser.ConfigParser.__init__(self,defaults)
        
    def __write_file(self, file):
        for setting in self.options('foo'):
            file.write('%s=%s\n' % (setting, self.get('foo',setting)))
        
    def write(self):
        self.add_section('foo')
        self.set('foo','ProxyHost',self.proxy_host)
        self.set('foo','ProxyPort',str(self.proxy_port))
        self.set('foo','ProxyMethod', str(session_types[self.proxy_method]))
        self.set('foo','ProxyUsername', self.proxy_username)
        self.set('foo','ProxyPassword', self.proxy_password)
        self.set('foo','ProxyTelnetCommand', self.proxy_command)
        self.set('foo','ProxyDNS',str(1))
        f=open(self.filename,'w')
        self.__write_file(f)
        f.close()
        
        